<?php

ini_set('include_path', '../');

require_once("lib/exception/duplicate_instance_exception.php");
require_once("lib/exception/internal_error_exception.php");
require_once("lib/exception/instance_not_found_exception.php");

require_once("util/configuration_parametters.php");

require_once("model/autor_message/to/autor_message_to.php");
require_once("model/autor_message/dao/autor_message_dao_factory.php");

require_once("model/autor/to/autor_to.php");
require_once("model/autor/dao/autor_dao_factory.php");

class Autor_message_dao_test {

	private $dao;
	private $autor1;
	private $autor2;
	
	public function __construct() {}	
	
	public function before_test() {
		try {
			$this->dao = Autor_dao_factory::singleton()->get_dao();
			$this->autor1 = new Autor_to('xxxxxxxxxxxx','password','email');
			$this->autor2 = new Autor_to('xxxxxxxxxxxy','password','email2');
			$this->dao->create($this->autor1);		
			$this->dao->create($this->autor2);
			$this->dao = Autor_message_dao_factory::singleton()->get_dao();
		}
		catch (Exception $e) {
			throw $e; 
		}
	}
	
	public function run_test() {		
		try {			
			$date = date("Y-m-d H:i:s");
			$autor_message_to = new Autor_message_to(0, $this->autor1->get_login(), $this->autor2->get_login(), 
														'title', 'content', $date, 'new');

			//create
			$autor_message_to = $this->dao->create($autor_message_to);	
			echo 'create autor_message: ' . $autor_message_to->to_string() . '<br/>';

			//create existent 
			try {
				$this->dao->create($autor_message_to);
				throw new Exception('create duplicate');
			}
			catch (Duplicate_instance_exception $e) {
				echo $e->getMessage() . '</br>';
			}
			
			//exist inexistent 
			if (!$this->dao->exist(9999))
				echo 'No exist description 9999<br/>';
			else 
				throw new Exception("Error, exist description inexistent");
			
			//exist existent 
			$this->dao->exist($autor_message_to->get_number());
			echo 'exist autor_message: ' . $autor_message_to->to_string() . '<br/>';
			
			//find inexistent 
			try {
				$this->dao->find_by_number(9999);
				throw new Exception('Found inexistent');
			}
			catch (Instance_not_found_exception $e) {
				echo $e->getMessage() . '</br>';
			}
			
			//find existent 
			$aux_autor_message_to = $this->dao->find_by_number($autor_message_to->get_number());	
				if ($autor_message_to->equals($aux_autor_message_to)) 
					echo 'found autor_message: ' . $autor_message_to->to_string() . '<br/>';
				else
					echo 'not equal autor_message inserted';
			
			//Count number inbox messages by autor
			$number = $this->dao->count_inbox_messages_by_login($this->autor2->get_login());
			echo 'Count number inbox messages by login: ' . $number . '</br>';
			
			//Count number inbox messages by autor
			$number = $this->dao->count_inbox_messages_by_login($this->autor1->get_login());
			echo 'Count number inbox messages by login: ' . $number . '</br>';
	
			//Count number new messages by login
			$number = $this->dao->count_new_messages_by_login($this->autor2->get_login());
			echo 'Count number new messages by login: ' . $number . '</br>';
			
			//Count number send messages by login
			$number = $this->dao->count_send_messages_by_login($this->autor1->get_login());
			echo 'Count number send messages by login: ' . $number . '</br>';
	
			//find inbox messages by autor
			$messages_list = $this->dao->find_inbox_by_login($this->autor2->get_login(), 0, 2);
				echo 'Inbox messages by autor: </br>';
				foreach ($messages_list as $message) 
					echo 'Message: ' . $message->to_string() . '</br>';
				
			//find send messages by autor
			$messages_list = $this->dao->find_send_by_login($this->autor1->get_login(), 0, 2);
				echo 'Send messages by autor: </br>';
				foreach ($messages_list as $message) 
					echo 'Message: ' . $message->to_string() . '</br>';
				
			//update 
			$autor_message_to->set_state("view");
			$this->dao->update($autor_message_to);
			$aux_autor_message_to = $this->dao->find_by_number($autor_message_to->get_number());
			echo 'update autor_message: ' . $aux_autor_message_to->to_string() . '<br/>';

			//remove inexistent 	
			try {
				$this->dao->remove(9999);
				throw new Exception('Found inexistent');
			}
			catch (Instance_not_found_exception $e) {
				echo $e->getMessage() . '</br>';
			}
			
			//remove existent 
			$this->dao->remove($autor_message_to->get_number());
			echo 'remove autor_message: ' . $autor_message_to->to_string() . '<br/>';

		}
		catch (Duplicate_instance_exception $e) {
			throw $e;
		}
		catch (Internal_error_exception $e) {
			throw $e;
		}
		catch (Instance_not_found_exception $e) {
			throw $e;
		}
		catch (Exception $e) {
			throw $e;
		}
	}
	
	public function after_test() {
		try {
			Autor_dao_factory::singleton()->get_dao()->remove($this->autor1->get_login());
			Autor_dao_factory::singleton()->get_dao()->remove($this->autor2->get_login());
		}
		catch (Exception $e) {
			throw $e;
		}
	}
}

//test
$test = new Autor_message_dao_test();

try {
	$test->before_test();
	$test->run_test();
	$test->after_test();
}
catch (Exception $e) {
	echo $e->getMessage();
}

?>